﻿/*
 * Created by SharpDevelop.
 * User: Alice
 * Date: 21.03.2012
 * Time: 08:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text;
using System.Timers;
using System.Security.Cryptography;

namespace monoServer
{
	/// <summary>
	/// Description of DataManager.
	/// </summary>
	public class DataManager
	{
		private int startValueSessionKey;
		private int sessionCounter;
		private ulong startValueTelegramKey;
		private ulong telegramCounter;
		private ulong threadCounter;
		private ulong connectorCounter;
		
		private Random randomGenerator;
		public static MD5 md5Generator = MD5.Create();
		
		private Object lock_currentsessionkey;
		public string currentSessionKey
		{
			get {
				lock(this.lock_currentsessionkey)
				{
					string date = DateTime.Now.ToString("yyyy-MM-dd");
					string tmp = date + "-" + sessionCounter.ToString() + "-" + this.getRandomIntString(100000,999999);
					this.sessionCounter++;
					return tmp;
				}
			}
			set {
			}
		}
		
		private Object lock_currenttelegramkey;
		public String currentTelegramKey
		{
			get {
				lock(this.lock_currenttelegramkey)
				{
					return (this.telegramCounter++).ToString();
				}
			}
		}
		
		private Object lock_currentthreadkey;
		public ulong curentThreadKey
		{
			get {
				lock(this.lock_currentthreadkey)
				{
					return this.threadCounter++;
				}
			}
		}
		
		private object lock_currentconnectorkey;
		public ulong currentConnectorKey
		{
			get {
				lock(this.lock_currentconnectorkey)
				{
					return this.connectorCounter++;
				}
			}
		}
				
		public DataManager()
		{
			this.startValueSessionKey = 100;
			this.startValueTelegramKey = 100;
			this.setup();
		}
		
		public DataManager(int startValueSessionKey, ulong startValueTelegramKey)
		{
			this.startValueSessionKey = startValueSessionKey;
			this.startValueTelegramKey = startValueTelegramKey;
			this.setup();
		}
		
		private void setup()
		{
			this.lock_currentsessionkey = new Object();
			this.sessionCounter = this.startValueSessionKey;
						
			this.lock_currenttelegramkey = new object();
			this.telegramCounter = this.startValueTelegramKey;
		
			this.lock_currentthreadkey = new object();
			this.threadCounter = 1000;
			
			this.lock_currentconnectorkey = new object();
			this.connectorCounter = 100;
			
			this.randomGenerator = new Random();
			DataManager.md5Generator = MD5.Create();
		}
		
		public void setSessionKeyToNull(object s, ElapsedEventArgs e)
		{
			this.sessionCounter = 0;
		}
		
		public static String getMD5(String t)
		{
			String lll = "";
			byte[] tmp = DataManager.md5Generator.ComputeHash(Encoding.Default.GetBytes(t));
			for(int i=0; i<tmp.Length; i++)
			{
				lll += tmp[i].ToString("x2");
			}
			return lll;
		}
		
		public static string getMD5(StringBuilder sb)
		{
			String lll = "";
			byte[] tmp = DataManager.md5Generator.ComputeHash(Encoding.Default.GetBytes(sb.ToString()));
			for(int i=0; i<tmp.Length; i++)
			{
				lll += tmp[i].ToString("x2");
			}
			return lll;
		}
		
		public String getRandomIntString(int min, int max)
		{
			return this.randomGenerator.Next(min, max).ToString();
		}
	}
}
